package com.cn.zaprk;

import com.cn.zaprk.beans.User;
import com.cn.zaprk.utils.DruidUtils;
import com.cn.zaprk.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.cn.zaprk
 * @ClassName: NextUpPageServlet
 * @Description:
 * @author: RZS
 * @date: 2022/4/24  19:20
 */
@WebServlet("/page_up_next.do")
public class NextUpPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 根据用户点击上一页或者下一页查询数据
        // 获取用户请求的分页数据信息
        Integer sumNumber = Integer.parseInt(request.getParameter("sumNumber"));// 获取总数
        Integer currentP = Integer.parseInt(request.getParameter("currentP"));// 获取当前页码
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));// 获取总页数
        Integer number = Integer.parseInt(request.getParameter("number"));// 获取每页显示的数量

        // 准备数据库连接信息
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet res = null;

        PageInfo<User> pageInfo = new PageInfo<>();
        List<User> list = new ArrayList<>();

        try {
            // 获取连接
            conn = DruidUtils.getConn();
            // 创建sql
            String sql = "select id, username, password, phone, email, salary from user limit ?, ?";
            // 创建预编译sql对象
            stat = conn.prepareStatement(sql);
            // 替换占位符
            stat.setInt(1, currentP * number);
            stat.setInt(2, number);
            // 执行sql，并且获取结果集
            res = stat.executeQuery();
            while (res.next()) {
                User user = new User();
                user.setId(res.getInt("id"));
                user.setUsername(res.getString("username"));
                user.setPassword(res.getString("password"));
                user.setPhone(res.getString("phone"));
                user.setEmail(res.getString("email"));
                user.setSalary(res.getDouble("salary"));

                // 添加到集合
                list.add(user);
            }

            // 封装pageInfo
            pageInfo.setSum(sumNumber);// 设置总条数
            pageInfo.setCurrentPage(currentP);// 设置当前页码
            pageInfo.setPageNumber(pageNum);// 设置总页数
            pageInfo.setInfo(list);// 设置分页查询结果集
            pageInfo.setNumber(number);// 设置每页显示的数量

            // 绑定到请求域对象中
            request.setAttribute("pageInfo", pageInfo);
            // 转发给JSP展示数据
            request.getRequestDispatcher("jsp/demo05.jsp").forward(request, response);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            DruidUtils.close(res, stat, conn);
        }
    }
}
